<?php
namespace app\admin\controller;
use think\Db;
/**
 * 商家
 * Class Member
 */
class ShopController extends BaseController {

    protected function _initialize() {
        parent::_initialize();
    }
	
	
	public function cate_goods($name=''){
		$where=[];
		if($name){
			$where['a.name']=array('like','%'.$name.'%');
		}
		$this->assign('name',$name);
		$list=db("good_cate")->where($where)->paginate(15);
		$this->assign('list',$list);
		return $this->fetch();
	}
	
	public function goods($is_on_sale='',$goods_name=''){
		$where=[];
		if($goods_name){
			$where['a.goods_name']=['like', '%'.$goods_name.'%'];
		}
		$this->assign('goods_name',$goods_name);
		if($is_on_sale){
			$where['a.is_on_sale']=$is_on_sale;
		}
		$this->assign('is_on_sale',$is_on_sale);
		$list=db("goods")->where($where)->paginate(15);
		$this->assign('list',$list);
		return $this->fetch();
	}
	
	
	//退货订单
	public function order_return($order_sn='',$status=''){
		 $where = [];
		if($order_sn){
			$where['order_sn']=['like', '%'.$order_sn.'%'];
		}
		if($status){
			$where['a.status']=$status;
		}
		$this->assign('status',$status);
		$this->assign('order_sn',$order_sn);
		$list=db("order_return")->alias("a")->join("member b","a.user_id=b.user_id")->where($where)->field("a.*,b.mobile")->paginate(15);
		$this->assign('list',$list);
		return $this->fetch();
	}
	//订单评价列表
	public function comments($order_sn='',$status=''){
		 $where = [];
		if($order_sn){
			$where['order_sn']=['like', '%'.$order_sn.'%'];
		}
		$this->assign('order_sn',$order_sn);
		if($status){
			$where['a.status']=$status;
		}
		$this->assign('status',$status);
		$list=db("good_comment")->alias("a")->join("good_order b","b.id=a.order_id")->where($where)->field("a.*,b.order_sn")->order("add_time desc")->paginate(15);
		$this->assign('list',$list);
		return $this->fetch();
	}
	//订单列表
    
    public function orders( $page = 1,$status='',$order_sn=''){
		$where=[];
		if($status){
			$where['m.status']=$status;
		}
		$this->assign('status',$status);
		
		if($order_sn){
			$where['m.order_sn']=['like', '%'.$order_sn.'%'];
		}
		$this->assign('order_sn',$order_sn);
		
		
	    $list = db('good_order')
		->where($where)
		->order("add_time desc")
	    ->paginate(3);
		
		$list1=array();
		if($list){
			foreach($list as $k=>$v){
				$list1[$v['id']]=$v;
			}
			$order_ids = array_keys($list1);						
			if($order_ids){
				$order_items = db("order_items")->where("order_id","in",$order_ids)->select();
				foreach($order_items as $k1=>$v1){
					 $list1[$v1['order_id']]['items'][]=$v1;
				}
			}
		}
		$this->assign("list1",$list1);
		$this->assign('list',$list);
		return $this->fetch();
    }
    
    //商家分类
	public function goods_type(){
		$list = db('goodstype')->select();
		$list=array2level($list);
		$this->assign('list',$list);
		return $this->fetch();
	}
	public function goodstype_add(){
		
		$list = db('goodstype')->select();
		$this->assign('list',$list);
		$this->assign('pid',input('pid'));
		return $this->fetch();
	}
	
	public function goodstype_edit(){
		 if(request()->isAjax()){
            $param = input('post.');         
         	if ($this->model->allowField(true)->save($param,$id) !== false) {
                    $this->success('修改成功', url('GoodsType/index'));
            } else {
                $this->error('修改失败');
            }
        }
        $id = input('param.id');
        $this->assign('model',$this->model->find($id));
        return $this->fetch();
	}
	
}
